﻿// HelpCommand.cs is part of AGatherBot.
//
// AGatherBot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 3 as
// published by the Free Software Foundation.
//
// AGatherBot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public version 2 License for more details.
//
// You should have received a copy of the GNU General Public License
// along with AGatherBot. If not, see <http://www.gnu.org/licenses/>.

using AGatherBot;
using System;
using System.Text;

namespace AGatherBot.Commands
{
    public class HelpCommand : ISharpCommand
    {
        public string CommandName
        {
            get
            {
                return ("Help");
            }
        }

        public string Execute(object sender, string input)
        {
            // Create a StringBuilder to store the response.
            StringBuilder sbHelp = new StringBuilder();

            // Split the given input to arguments.
            string[] strarrArgs = input.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            // If no arguments was supplied, throw an exception.
            if (strarrArgs.GetLength(0) == 0)
            {
                throw new NotImplementedException();
            }
            // Else, if there was no specific command help request, display the available commands.
            else if (strarrArgs.GetLength(0) == 1)
            {
                sbHelp.AppendLine("Available Commands:");
                sbHelp.AppendLine("-------------------");

                // Go over each command and print it to the user.
                foreach (String strCmd in Program.Commands.Keys)
                {
                    sbHelp.AppendLine("\t" + strCmd);
                }
            }
            // Else, if specific command was requested, display its specific help text.
            else if (strarrArgs.GetLength(0) == 2)
            {
                if (!Program.Commands.ContainsKey(strarrArgs[1]))
                {
                    sbHelp.AppendLine("No help text was found for requested command: " + strarrArgs[1]);
                }
                else
                {
                    sbHelp.AppendLine("Help for command: " + strarrArgs[1]);

                    sbHelp.AppendLine(Program.Commands[strarrArgs[1]].Help(sender, string.Empty));
                }
            }

            return (sbHelp.ToString());
        }

        public string Help(object sender, string input)
        {
            var helpText    = "Usage: Help [<Command>]" + Environment.NewLine +
                                "Displays the available commands or the given command's help text.";

            return (helpText);
        }
    }
}